<?php
    session_start();

    if (!isset($_SESSION['is_logged_in'])) {
        header("Location:../index.php");
        die();
    }
    
    ini_set('display_errors',"1");
    include("conexion.php");
    
    $operacion = $_POST["operacion"];
    $NumeTorneo = $_POST["NumeTorneo"];
    $NombTorneo = $_POST["NombTorneo"];
	$ImpoInsc = $_POST["ImpoInsc"];
	$ImpoPart = $_POST["ImpoPart"];
	$NumeEsta = $_POST["NumeEsta"];
    
    
    $conn = new mysqli($dbhost, $dbuser, $dbpass, $db) or die("Problemas al conectar con la BD");
    
    switch ($operacion) {
        case 0:
            $strSQL = "SELECT COALESCE(MAX(NumeTorneo), 0) + 1 NumeTorneo FROM torneos";
            $tabla = $conn->query($strSQL);
            $fila = $tabla->fetch_array();
            
            $strSQL = "INSERT INTO torneos(NumeTorneo, NombTorneo, ImpoInsc, ImpoPart, NumeEsta)";
            $strSQL.= " VALUES({$fila["NumeTorneo"]}, '{$NombTorneo}', {$ImpoInsc}, {$ImpoPart}, {$NumeEsta})";

            if (!$conn->query($strSQL))
                echo "Fall&oacute; INSERT:<br />(" . $conn->errno . ") " . $conn->error . "<br />" . $strSQL;
            else
                echo "INSERT Correcto!";                
            
            $tabla->free();
            break;
        
        case 1:
            $strSQL = "UPDATE torneos";
            $strSQL.= " SET NombTorneo = '" . $NombTorneo . "'";
            $strSQL.= ", ImpoInsc = " . $ImpoInsc;
            $strSQL.= ", ImpoPart = " . $ImpoPart;
            $strSQL.= ", NumeEsta = " . $NumeEsta;
            $strSQL.= " WHERE NumeTorneo = " . $NumeTorneo;
            
            if (!$conn->query($strSQL))
                echo "Fall&oacute; UPDATE:<br />(" . $conn->errno . ") " . $conn->error . "<br />" . $strSQL;
            else
                echo "UPDATE Correcto!";
            
            break;
            
        case 2:
            $strSQL = "DELETE FROM torneos WHERE NumeTorneo = " . $NumeTorneo;
            if (!$conn->query($strSQL))
                echo "Fall&oacute; DELETE:<br />(" . $conn->errno . ") " . $conn->error . "<br />" . $strSQL;
            else
                echo "DELETE Correcto!";
            
            break;
    }
    $conn->close();
?>